//variable decleration
unsigned int wdt_ctr = 0;
unsigned long int timer_counter32;
unsigned char bufferx[100];
unsigned char LLID_table[20][2] = { {0,1},//ONU1
                               {0,2},//ONU2
                               {0,3},//ONU3
                               {0,4},//ONU4
                               {0,5},//ONU5
                               {0,6},//ONU6
                               {0,7},//ONU7
                               {0,8},//ONU8
                               {0,9},//ONU9
                               {0,10},//ONU10
                               {0,11},//ONU11
                               {0,12},//ONU12
                               {0,13},//ONU13
                               {0,14},//ONU14
                               {0,15},//ONU15
                               {0,16},//ONU16
                               {0,17},//ONU17
                               {0,18},//ONU18
                               {0,19},//ONU19
                               {0,20},//ONU20
                             };                             
unsigned char OLT_MACAddr[6] = {0x00,0x26,0x82,0x21,0x36,0x8C};
unsigned char MACAddrTable[20][6] = { {0x01,0x02,0x03,0x04,0x05,0x06},//ONU1
                                      {0x02,0x08,0x09,0x0A,0x0B,0x0C},//ONU2
                                      {0x03,0x0F,0x10,0x11,0x12,0x13},//ONU3
                                      {0x04,0x15,0x16,0x17,0x18,0x19},//ONU4
                                      {0x05,0x02,0x03,0x04,0x05,0x06},//ONU5
                                      {0x06,0x02,0x03,0x04,0x05,0x06},//ONU6
                                      {0x07,0x02,0x03,0x04,0x05,0x06},//ONU7
                                      {0x08,0x02,0x03,0x04,0x05,0x06},//ONU8
                                      {0x09,0x02,0x03,0x04,0x05,0x06},//ONU9
                                      {0x0A,0x02,0x03,0x04,0x05,0x06},//ONU10
                                      {0x0B,0x02,0x03,0x04,0x05,0x06},//ONU11
                                      {0x0C,0x02,0x03,0x04,0x05,0x06},//ONU12
                                      {0x0D,0x02,0x03,0x04,0x05,0x06},//ONU13
                                      {0x0E,0x02,0x03,0x04,0x05,0x06},//ONU14
                                      {0x0F,0x02,0x03,0x04,0x05,0x06},//ONU15
                                      {0x10,0x02,0x03,0x04,0x05,0x06},//ONU16
                                      {0x11,0x02,0x03,0x04,0x05,0x06},//ONU17
                                      {0x12,0x02,0x03,0x04,0x05,0x06},//ONU18
                                      {0x13,0x02,0x03,0x04,0x05,0x06},//ONU19
                                      {0x14,0x02,0x03,0x04,0x05,0x06},//ONU20
                                    };

/*********************************************************************************************     
grand list format <0> 0 - no grants
                      1 - grants ready
                      2 - grant process
                      3 - grant send
                  <1> - number of grants
         <2><3><4><5> - grant#1 start time
               <6><7> - grant#1 length
       <8><9><10><11> - grant#2 start time
             <12><13> - grant#2 length
     <14><15><16><17> - grant#3 start time
             <18><19> - grant#3 length
     <20><21><22><23> - grant#4 start time
             <24><25> - grant#4 length
***********************************************************************************************/  
unsigned char grants_reg_status; //0 - availabe for process
                                 //1 - in used             
unsigned char grants_list[20][26] ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU1
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU2
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU3
                                    {0,0,0,4,5,6,7,8,1,2,3,4,5,6,7,8,9,1,2,3,5,5,6,9,7,6},//ONU4
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU5
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU6
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU7
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU8
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU9
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU10
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU11
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU12
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU13
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU14
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU15
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU16
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU17
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU18
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU19
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU20
                                   };
/************************************************************************************************
Report list format <0> - 0 - no report
                         1 - report avalible
                   <1> - no of Q set
                   <2> - report bitmap
                   <3> - Q1 byte 0
                   <4> - Q1 byte 1
*************************************************************************************************/

unsigned char report_list[20][100]={{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,20},//ONU1
                                    {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU2
                                    {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU3
                                    {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU4
                                    {5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU5
                                    {6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU6
                                    {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU7
                                    {8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU8
                                    {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU9
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU10
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU11
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU12
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU13
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU14
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU15
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU16
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU17
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU18
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU19
                                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//ONU20
                                   };

unsigned char EthernetMPCP[73];
long x;
long  grant_alc;

unsigned char in1_buffer[400];
unsigned char in2_buffer[100];
unsigned char URX1_Ready = 0 ;
unsigned char URX2_Ready = 0 ;
unsigned int in_ctr = 0;
unsigned int in_ctr1 = 0;
unsigned int length_type = 0x0000;



//Define
#define onu_ctr  18
#define wdt_led  PORTDbits.RD0
#define DBA_Ready  PORTGbits.RG2
#define SPD  0x02
#define Reserved  0x55
#define SLD  0xD5
#define CSDBA  _RE5
#define TCSDBA  _TRISE5
#define sd  PORTBbits.RB0
#define TX2EN U2STAbits.UTXEN 
#define processing_delay 41494



//function decleration
void init_timer1(void);
void init_uart1(void);
void init_uart2(void);
void wdt_(void);
void buffer_clr(void);
void GATE_Normal(unsigned char DA_pointer,unsigned char flags, unsigned char *data);
void grant_gen_1st_boot(void);




//external function declaration
/*transform.c*/
extern int changeByte2Integer(unsigned char msb,unsigned char lsb);
extern int changeByte2Int(unsigned char msb,unsigned char lsb);
extern long changeByte2Long(unsigned char byte1,unsigned char byte2,unsigned char byte3,unsigned char byte4);
extern unsigned char *changeLong2Byte(long DataVal);
extern unsigned char *changeInt2Byte(unsigned long data);
/*process_data.c*/
extern void process_URX1(void);
extern void process_URX2(void);
extern void capture_delay(unsigned char capture);//from monitor.c
extern void dba(void); //from dba.c
